package com.lxb.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lxb.eduservice.entity.EduChapter;
import com.lxb.eduservice.entity.EduVideo;
import com.lxb.eduservice.mapper.EduChapterMapper;
import com.lxb.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxb.eduservice.service.EduVideoService;
import com.lxb.eduservice.vo.chapter.ChapterVo;
import com.lxb.eduservice.vo.chapter.VideoVo;
import com.lxb.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 课程章节 服务实现类
 *
 * @author lixuebin
 * @since 2022-11-06
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService eduVideoService;

    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String id) {
        //存放返回数据
        List<ChapterVo> chapterVoList = new ArrayList<>();

        //1.根据课程id查询出所有的课程章节信息
        QueryWrapper<EduChapter> queryWrapperOne = new QueryWrapper<>();
        queryWrapperOne.eq("course_id", id);
        List<EduChapter> chapters = baseMapper.selectList(queryWrapperOne);

        //2.根据每个章节id，查询到小节信息
        QueryWrapper<EduVideo> queryWrapperTwo = new QueryWrapper<>();
        queryWrapperTwo.eq("course_id", id);
        List<EduVideo> eduVideos = eduVideoService.list(queryWrapperTwo);

        //3.封装数据
        for (EduChapter chapter : chapters) {

            //封装章节数据
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(chapter, chapterVo);
            chapterVoList.add(chapterVo);

            List<VideoVo> videoVoList = new ArrayList<>();
            //封装与该章节id，匹配的小节数据
            for (EduVideo eduVideo : eduVideos) {
                //判断小节与章节id是否一致
                if (eduVideo.getChapterId().equals(chapter.getId())) {
                    //创建课程视频对象
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo, videoVo);
                    videoVoList.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVoList);
        }
        return chapterVoList;
    }


    @Override
    public boolean deleteChapter(String chapterId) {

        //根据章节id，查询小节信息。如果有数据，则不进行删除操作
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("chapter_id", chapterId);
        //获取小节的数据，videCount代表多少条数据
        int videCount = eduVideoService.count(queryWrapper);

        if (videCount > 0) {
            //有小节数据
            throw new GuliException(20001, "不能删除改章节");
        } else {
            //没有小节数据，可以删除该章节
            Integer result = baseMapper.deleteById(chapterId);
            return result > 0;
        }
    }

    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        baseMapper.delete(queryWrapper);
    }

}
